package com.ludomancy.insane;

import java.awt.Graphics;
import java.awt.Point;
import java.util.Vector;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.entity.EntityRadiusFilter;

public class Player extends InsaneEntity
{
	public static final int PLAYER_SPEED = 100;
	private Point target = new Point();
	public boolean activated = false;
	public InsaneEntity inventory;
	private ImageStrip sprite;
	private ImageAnimation animation;
	
	public Player(Point pos)
	{
		super(pos);
		sprite = new ImageStrip(ResourceManager.getImage("char.png"), 8, 8);
		animation = new ImageAnimation(sprite,160);
		animation.play();
		animation.looping = true;
	}
	
	public void setTarget(Point target)
	{
		this.target.setLocation(target);
	}

	public void paint(Graphics g, int x, int y)
	{
		if(inventory != null)
		{
			int offsetX = (screenSize().x - inventory.screenSize().x)/2;
			inventory.paint(g, x + offsetX, y - inventory.screenSize().y - 2);
		}
		animation.paintCurrentFrame(g, x, y);
	}
	
	public void update()
	{
		animation.update();
		pos = getNextPos(target, PLAYER_SPEED);
		// anything new to pick?
		Vector near = parent.getEntities(new EntityRadiusFilter(this, PLAYER_SPEED));
		for (int i = 0; i < near.size(); i++)
		{
			InsaneEntity e = (InsaneEntity) near.elementAt(i);
			if(e.isPickable() && e != inventory)
			{
				if(inventory != null)
				{
					// drop
					inventory.visible = true;
					// TODO: should make this better
					inventory.pos.setLocation(new Point(Randomizer.getRandom(100,parent.width), Randomizer.getRandom(100,parent.height)));
				}
				inventory = e;
				inventory.visible = false;
				break;
			}
		}
		
		if(activated && inventory != null)
		{
			inventory.apply(pos);
		}
	}
}
